package com.example.redisdemo;

import com.example.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class TestController {
    @Autowired
    RedisUtil redisUtil;

    @RequestMapping("/test/cache")
    @Cacheable(value = "cache", key = "#id")
    public User cache(@RequestParam Integer id) {
        User user = new User("小红ss", 20, new Play("篮球", true));
        return user;
    }

    @RequestMapping("/test/evict")
    @CacheEvict(value = "cache", allEntries = true)
    public String evict() {
        return "ok";
    }

    @RequestMapping("/test/hash")
    public void hash() {
        redisUtil.hmSet("hash_key", "key_0", "0");
        redisUtil.hmSet("hash_key", "key_1", "1");
        redisUtil.hmSet("hash_key", "key_2", "2");
        redisUtil.hmSet("hash_key", "key_3", "3");
    }

    @RequestMapping("/test/hashGet")
    public void hashGet() {
        Map<Object, Object> value = redisUtil.hmGetAll("hash_key");
        value.keySet().forEach(k -> System.out.println(k + "：" + value.get(k)));
    }
}
